<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>恒铸智能柜管理平台</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/resources/layui/css/layui.css"
	media="all" />
<script type="text/javascript"
	src="${pageContext.request.contextPath}/resources/layui/layui.js"></script>
</head>
<style>
.layui-form-item {
	display: inline-block;
	margin-bottom: 0px;
}
</style>
<body class="childrenBody">
	<form class="layui-form">
		<blockquote class="layui-elem-quote admin_search">
			<div>
				<a class="layui-btn addbtn" id="addbtn">新增</a>
			</div>
		</blockquote>
		<input type="hidden" id="compId" value="${compId}">
		<div class="layui-form-item selectComp">
			<label class="layui-form-label">公司:</label>
			<div class="layui-input-block">
				<select name="interest" lay-filter="selectComp">
					<option value="">全部</option>
					<c:forEach items="${comps }" var="list">
						<option value="${list.compId }">${list.compName }</option>
					</c:forEach>
				</select>
			</div>
		</div>
		<div class="demoTable layui-form-item like">
			<label class="layui-form-label">搜索：</label>
			<div class="layui-inline">
				<input class="layui-input" id="like" name="like" autocomplete="off"
					placeholder="请输入位置或描述">
				<dl style="display: none;"></dl>
			</div>
			<a class="layui-btn" style="margin-top: -4px;">搜索</a>
		</div>
		<table id="adList" lay-filter="adList"></table>

		<!--操作-->
		<script type="text/html" id="publish">
		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
	</script>
	</form>
	<script>
	layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    if($("#compId").val()==0){
    	$(".selectComp").show();
    }else{
    	$(".selectComp").hide();
    }
    	//公司
     form.on("select(selectComp)",function(data){
    	 id = data.value;
 		//执行重载
    	  table.reload('adListTable', {
			  url: '/publish/list'
			  ,page: {
		          curr: 1 //重新从第 1 页开始
		        }
			  ,where: {
				  compIdParam: id
			  } 
			});
    })
    
   /*  $("#like").on("input propertychange",function(){
    	$(".like dl dd").remove();
    	$(".like dl").hide();
    	$.post("/publish/ajax?like="+$(this).val(),function(data){
    		if(data.length>0){
    			$(".like dl").show();    			
	    		for(var i=0;i<data.length;i++){
		    		$(".like dl").append('<dd class="layui-select-tips">'+data[i]+'</dd>')
	    		}
    		}
    	})
   	})
   	$("body").on("click",function(){
   		$(".like dl").hide();
   	})
   	$(".like").on("click","dl dd",function(){
		$("#like").val($(this).html())
		$(".like dl").hide();
	}) */
    
    $(".like").on("click","a",function(){
    	var like = $(".like input").val();
    	 table.reload('adListTable', {
			  url: '/publish/list'
			  ,page: {
		          curr: 1 //重新从第 1 页开始
		        }
			  ,where: {
				 like: "%"+like+"%"
			  } 
			});
    })
    
    //列表
    var tableIns = table.render({
        elem: '#adList',
        url : '/publish/list',
        cellMinWidth : 95,
        page : true,
        height : "full-145",
		method : 'post',
        limit : 20,
        limits : [10,15,20,25],
        id : "adListTable",
        cols : [[
            {field: 'caseNo', title: '储物柜的ID',  align:'center', sort: true,templet: function(d){
        		var str="";
        		if(isNotNull(d.place)||isNotNull(d.caseNoBuy)){
        			isNotNull(d.caseNoBuy)?str="号柜":1+1;
        			isNotNull(d.place)?1+1:d.place="";
        			return  d.place +'<span style="color:#FF5722;margin-left:6px">'+ d.caseNoBuy +'</span>'+str 
        		}else{
        			return  d.caseNo 
        		}
        	}},
            {field: 'adName', title: '广告名称', align:'center', sort: true},
            {field: 'compName', title: '所属公司', align:'center', sort: true},
            {field: 'creatTime', title: '创建时间', align:'center'},
            {field: 'des', title: '描述', align:'center', sort: true},
            {field: 'place', title: '位置', align:'center', sort: true},
            {title: '操作', width:170, templet:'#publish',fixed:"right",align:"center"}
        ]]
    });
   //空字符串校验
     function isNotNull(obj){
 	    var result = true;
 	    switch (obj) {
 	        case undefined:
 	        case null:
 	            result = false;
 	            break;
 	    }
 	    if(result){
 	        if(obj.replace(/\s+/g,"") == ""){//空字符串校验
 	            result = false;
 	        }
 	    }
 	    return result;
 	}
    function addAd(edit){
        var index = layui.layer.open({
            title : "广告编辑",
            type : 2,
            content : "/publish/edit?fileId="+edit.fileId,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                	body.find(".id").val(edit.id);
                    body.find(".caseNo").val(edit.caseNo);
                    body.find(".des").val(edit.des);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回广告列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addbtn").click(function(){
    	var index = layui.layer.open({
            title : "广告添加",
            type : 2,
            content : "/publish/add",
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回广告列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    })
    //列表操作
    table.on('tool(adList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addAd(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此广告？',{icon:3, title:'提示信息'},function(index){
            	$.post("/publish/deleteAdvertise?id="+data.id,function(rest){
            		layer.msg(rest.message);
            	})
                tableIns.reload();
                layer.close(index);
            });
        }
    });

})
</script>
</body>
</html>